package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IDEA
 * author:zhiXiang-Chen
 * Date:2019/3/2
 * Time:9:52
 */
@CrossOrigin
@RestController
@RequestMapping("/spit")
public class SpritController {
    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Spit spit){
         spitService.add(spit);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Spit> list =  spitService.findAll();
        return  new Result(true, StatusCode.OK,"查询成功",list);
    }

    @RequestMapping(value = "/{spitId}",method = RequestMethod.GET)
    public Result findOne(@PathVariable("spitId") String spitId){
        Spit spit =   spitService.findById(spitId);
        return  new Result(true, StatusCode.OK,"查询成功",spit);
    }

    @RequestMapping(value = "/{spitId}",method = RequestMethod.PUT)
    public Result update(@PathVariable("spitId") String spitId,@RequestBody Spit spit){
        spit.set_id(spitId);
        spitService.update(spit);
        return  new Result(true, StatusCode.OK,"更新成功",spit);
    }

    @RequestMapping(value = "/{spitId}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("spitId") String spitId){
        spitService.deleteById(spitId);
        return  new Result(true, StatusCode.OK,"删除成功");
    }

    @RequestMapping(value = "/comment/{parentId}/{page}/{size}",method = RequestMethod.GET)
    public Result findByParentId(@PathVariable("parentId") String parentId,@PathVariable("page") int page, @PathVariable("size") int size){
        Page<Spit> pageList =  spitService.findByParentId(parentId,page,size);
        PageResult<Spit> pageResult = new PageResult<>(pageList.getTotalElements(), pageList.getContent());
        return  new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 点赞方法
     * @param spitId
     * @return
     */
    @RequestMapping(value = "/thumbup/{spitId}",method = RequestMethod.PUT)
    public Result updateThumbup(@PathVariable("spitId") String spitId){

        String userId="123"; // 模拟一个用户id  后续做了用户模块再修改
        if (redisTemplate.opsForValue().get("spit_"+userId+"_"+spitId)!=null){
            return  new Result(false, StatusCode.REPERROR,"不能重复点赞哦");
        }

        spitService.updateThumbup(spitId);
        //点赞成功后,在redis中插入一个记录
        redisTemplate.opsForValue().set("spit_"+userId+"_"+spitId,"1");
        return  new Result(true, StatusCode.OK,"点赞成功");
    }
}
